﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanLyHangHoa : UserControl
    {
        public QuanLyHangHoa()
        {
            InitializeComponent();
        }

        private void QuanLyHangHoa_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvHangHoa.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var HangHoas = from i in dc.HangHoas where i.deleted == false select i;
            int count = 1;
            foreach (_DB.HangHoa i in HangHoas)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvHangHoa);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = i.MaHangHoa;
                row.Cells[2].Value = i.TenHangHoa;
                row.Cells[3].Value = (
                    from hn in dc.HangHoa_NhaCungCaps
                    where hn.MaHangHoa == i.MaHangHoa &&
                          hn.Dat == true &&
                          hn.PhieuDanhGiaNCC.TrangThai == _DB.TrangThaiPhieu.DA_DUYET_ID &&
                          hn.PhieuDanhGiaNCC.deleted == false
                    select hn.MaNhaCungCap).Distinct().Count();

                dgvHangHoa.Rows.Add(row);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvHangHoa.SelectedRows.Count > 0)
            {
                string maHangHoa = (string)dgvHangHoa.SelectedRows[0].Cells[1].Value;
                OtherForms.ThemHangHoaForm f = new OtherForms.ThemHangHoaForm(maHangHoa);
                f.ShowDialog();
                loadData();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OtherForms.ThemHangHoaForm f = new OtherForms.ThemHangHoaForm();
            f.ShowDialog();
            loadData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dgvHangHoa.SelectedRows.Count == 0)
                return;
            if (MessageBox.Show("Chắc chắn xóa?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            foreach (DataGridViewRow row in dgvHangHoa.SelectedRows)
            {
                string maHangHoa = (string)row.Cells[1].Value;
                //string maHangHoa = (string)row.Tag;

                _DB.HangHoa HangHoa = (from i in dc.HangHoas
                                 where (i.MaHangHoa == maHangHoa)
                                 select i).Single();
                if(HangHoa!=null)
                    HangHoa.deleted = true;
            }

            dc.SubmitChanges();
            loadData();
        }

        private void dgvHangHoa_DoubleClick(object sender, EventArgs e)
        {
            if (dgvHangHoa.SelectedRows.Count > 0)
            {
                string maHangHoa = (string)dgvHangHoa.SelectedRows[0].Cells[1].Value;
                OtherForms.XemHangHoaForm f = new OtherForms.XemHangHoaForm(maHangHoa);
                f.ShowDialog();
            }
        }

       


    }
}
